/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.fragment;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.NiudanAccount;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.activity.NDNiudanActivity;
import com.simon.mengkou.ui.base.BaseNiudanFragment;
import com.simon.mengkou.ui.view.StrokeTextView;
import com.simon.mengkou.utils.UtilOuer;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/1/6.
 * desc    : 扭蛋中心界面
 */
public class NDCenterFragment extends BaseNiudanFragment {
    //头像
    @Bind(R.id.center_id_avatar) SimpleDraweeView mSdvAvatar;
    //纽币
    @Bind(R.id.center_id_score) TextView mTvScore;
    //昵称
    @Bind(R.id.center_id_nick)
    StrokeTextView mStvNick;

    @Override
    protected void initLayout() {
        setContentView(R.layout.fragment_center);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this, getView());
        registerAction(CstOuer.BROADCAST_ACTION.NIUDAN_PLAY_ACTION);

        //打开门
        setDoorVisible(true);
        openDoor(null, 500);

        //显示头像
        OuerApplication.mImageLoader.loadCircleImage(mSdvAvatar, OuerApplication.mUser.getAvatarUrl());
        //显示个人纽扣
        mTvScore.setText(getString(R.string.center_score,  OuerApplication.mUser.getNiudanAccount() == null
                ? 0 : OuerApplication.mUser.getNiudanAccount().getNiukou() ));
        //显示昵称
        mStvNick.setText(OuerApplication.mUser.getNick());

        if(OuerApplication.mUser.getNiudanAccount() == null) {
            getNiudanAccount();
        }
    }

    /**
     * 扭蛋图鉴
     */
    @OnClick(R.id.center_id_image)
    public void presentImage(){
        closeDoor(new NDNiudanActivity.OnDoorCloseListener() {
            @Override
            public void onClose() {
                OuerDispatcher.presentImageFragment(mActivity);
            }
        });
    }

    /**
     * 扭蛋交易所
     */
    @OnClick(R.id.center_id_trade)
    public void presentTrade(){
        UtilOuer.toast(mActivity, R.string.common_feature_disable);
    }

    /**
     * 扭券库
     */
    @OnClick(R.id.center_id_coupon)
    public void presentCoupon(){
        closeDoor(new NDNiudanActivity.OnDoorCloseListener() {
            @Override
            public void onClose() {
                OuerDispatcher.presentCouponFragment(mActivity);
            }
        });
    }

    /**
     * 扭蛋列表
     */
    @OnClick(R.id.center_id_machine)
    public void presentOptions(){
        closeDoor(new NDNiudanActivity.OnDoorCloseListener() {
            @Override
            public void onClose() {
                OuerDispatcher.presentOptionsFragment(mActivity);
            }
        });

    }


    /**
     * 获取我的扭券库
     */
    private void getNiudanAccount() {
        AgnettyFuture future = OuerApplication.mOuerFuture.getNiudanAccount(
                OuerApplication.mUser.getUserId(),
                new OuerFutureListener(mActivity) {
                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        NiudanAccount account = (NiudanAccount) result.getAttach();
                        OuerApplication.mUser.setNiudanAccount(account);
                        //显示个人纽扣
                        mTvScore.setText(getString(R.string.center_score, account == null
                                ? 0 : account.getNiukou() ));
                    }
                });

        attachDestroyFutures(future);
    }


    @Override
    public void onBackPressed() {
        final FragmentManager fm = mActivity.getSupportFragmentManager();
        if(fm.getBackStackEntryCount() == 0){
            closeDoor(new NDNiudanActivity.OnDoorCloseListener() {
                @Override
                public void onClose() {
                    finish();
                }
            });

        }else{
            closeDoor(new NDNiudanActivity.OnDoorCloseListener() {
                @Override
                public void onClose() {
                    fm.popBackStack();
                    openDoor(null);
                }
            });

        }
    }

    @Override
    protected void onReceive(Intent intent) {
        super.onReceive(intent);
        if(CstOuer.BROADCAST_ACTION.NIUDAN_PLAY_ACTION.equals(intent.getAction())) {
            getNiudanAccount();
        }
    }
}
